package web.servlets;

import domain.Account;
import service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class
UpdateInformationServlet extends HttpServlet {

    private static final String VIEW_EIDTACCOUNTFORM="/WEB-INF/jsp/account/EditAccountForm.jsp";
    private static final String VIEW_SIGNONFORM="/WEB-INF/jsp/catalog/Main.jsp";
    private static final String ERROR_MESSAGE="/WEB-INF/jsp/common/Error.jsp";
    Account account;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        System.out.println();
        HttpSession session = req.getSession();
        account = (Account) session.getAttribute("account");
        AccountService accountService = new AccountService();

        String username = account.getUsername();
        String password = req.getParameter("password");
      String  repeatedPassword= req.getParameter("repeatedPassword");
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address1 = req.getParameter("address1");
        String address2 = req.getParameter("address2");
        String city = req.getParameter("city");
        String state = req.getParameter("state");
        String zip = req.getParameter("zip");
        String country = req.getParameter("country");
        String languagePreference = req.getParameter("languagePreference");
        String favouriteCategoryId = req.getParameter("favouriteCategoryId");
        String listOption = req.getParameter("listOption");
        String bannerOption = req.getParameter("bannerOption");
        if(!"".equals(username)&&!"".equals(password)&&!"".equals(repeatedPassword)&&!"".equals(email)&&!"".equals(firstName)&&!"".equals(lastName)&&
                !"".equals(address1)&&!"".equals(address2)&&!"".equals(city)&&!"".equals(state)&&!"".equals(zip)&&
                !"".equals(country)&&!"".equals(phone)&&!"".equals(languagePreference)) {
            if (password.equals(repeatedPassword)) {
                account.setUsername(username);
                account.setPassword(password);
                account.setFirstName(firstName);
                account.setLastName(lastName);
                account.setEmail(email);
                account.setPhone(phone);
                account.setAddress1(address1);
                account.setAddress2(address2);
                account.setCity(city);
                account.setState(state);
                account.setZip(zip);
                account.setCountry(country);
                account.setLanguagePreference(languagePreference);
                account.setFavouriteCategoryId(favouriteCategoryId);
                account.setListOption(Boolean.parseBoolean(listOption));
                account.setBannerOption(Boolean.parseBoolean(bannerOption));

                accountService = new AccountService();
                accountService.updateAccount(account);
                //System.out.println("account "+account);
                session.setAttribute("account", account);

                req.getRequestDispatcher(VIEW_SIGNONFORM).forward(req, resp);
            } else {

                session.setAttribute("message", "Failed! The entered passwords are inconsistent,Ensure that the passwords entered are the same!");
                req.getRequestDispatcher(ERROR_MESSAGE).forward(req, resp);
            }
        }
          else {

        session.setAttribute("message","Failed! The information form must not be blank!");
        req.getRequestDispatcher(ERROR_MESSAGE).forward(req,resp);
       }
    }

    }



